package com.itheima.d4_synchronized_code;

class Ticket02 extends Thread {
    private int ticket = 100;

    @Override
    public void run() {
        while (ticket > 0) {
            setTicket();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }

    public synchronized void setTicket() {
        if (ticket > 0) {
            ticket--;
            System.out.println(Thread.currentThread().getName() + " 在卖票，剩余票数为" + ticket);
        }
    }
}
